/**
 * X.U.E frameworks
 * X : xue	学而思基础库
 * U : UI	学而思UI库
 * E : extend	学而思扩展
 * Copyright 2012 Xueersi Inc. All rights reserved.
 */

var X, x, xue = X = x = xue || {
	version : '1.0.1'
};
/**
 * 加载script文件
 * @param {String} 		url
 * @param {Function}	callback
 * @param {Boolean}		isfoot
 */
xue.loadJS = function( url, callback, isfoot, opt ){
	var _arg = arguments, _url, _call, _opt, _inFoot;
	/**
	 * 判断参数是否存在
	 * url 		{string}
	 * callback	{function}
	 * opt		{object}
	 */
	if(_arg.length == 0){ return; }
	
	for( var i = 0, len = _arg.length; i < len; i++ ){
		var __a = _arg[i], __tp = typeof __a;
		
		if(__tp == 'string'){			// 类型为string时则为url
			_url = __a;
		}else if(__tp == 'function'){	// 类型为function时则为回调函数
			_call = __a;
		}else if(__tp == 'object'){		// 类型为object时则为配置参数
			_opt = __a;
		}else if(__tp == 'boolean'){
			_inFoot = __a;
		}else{
			return;
		}
	}
		
	if(!_url) { return; }
	
	var _callback = function(){	return _call ? _call() : ''; };
	
	// 检查是否存在函数，返回布尔值；
	var _isLoad = false;
	(function(){
		var _script = document.getElementsByTagName('script');
		for(var i = 0, len = _script.length; i < len; i++) {
			if(_script[i].src.indexOf(_url) > -1) {
				_isLoad = true;
			}
		}
		_isLoad = false;
	})();	
	// 检测是否存在，不存在则加载，否则直接返回callback
	if(!_isLoad){
		var _head = document.getElementsByTagName('HEAD')[0],
			_body = document.getElementsByTagName('BODY')[0],
			_inserWrap = _inFoot ? _body : _head;
			
		var s = document.createElement('script');
		s.type = 'text/javascript';
		s.src = _url;
		_inserWrap.appendChild(s);
		
		if(s.readyState) {// IE
			s.onreadystatechange = function() {
				if(script.readyState == 'loaded' || script.readyState == 'complete') {
					script.onreadystatechange = null;
					_callback();
				}
			};
		} else {
			s.onload = function() {
				_callback();
			};
		}			
	}else{
		_callback();
	}

	return this;
};
/**
 * 主机域名
 */
xue.domain = function() {
	var _host = document.location.host;
	return (_host == '' || _host == 'www.xes.com') ? 'www.xueersi.com' : _host;
}();

/**
 * Load $LAB framework
 * xue.load = $LAB
 * 
 * xue.load.script(url).wait(function(){
 * 	
 * });
 */
xue.LabReady = function( callback ){
	xue.loadJS('js/LAB.js',function(){
	    xue.load = $LAB;
	    if(callback){
	    	callback();
	    }
	});
};
/**
 * xue库基础加载完毕，既加载完jQuery之后的状态
 */
xue.ready = function( callback ){
	
	//加载页头统计代码
	xue.loadJS('/js/xue.app.log.js');
	
	//加载jQuery库文件
	xue.loadJS('/js/jquery-1.4.2.min.js',function(){
	    if( typeof callback == 'function' ){
	    	callback();
	    }
	    //这里放的是需要在页面加载完成之后运行的统计代码
		$(function(){
			try {
			  var idgTracker = _alysP.init("idigger","T-000048-01", "");
			  idgTracker._setTrackPath('http://idigger.allyes.com/main/adftrack');
			  idgTracker._trackPoint();
			} catch(err) {}
			
			xue.loadJS('http://stats.eduuu.com/logs.js',true)
				.loadJS('http://analysis.xueersi.com/statistic/js/statistic_src.js?' + loadStartTime,true);		
		});	    
	});
};
